package org.setaria.control.file
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	
	import mx.collections.ArrayCollection;
	import mx.core.IVisualElement;
	
	import spark.components.Button;
	import spark.components.DataGroup;
	import spark.components.Label;
	import spark.components.List;
	import spark.components.Scroller;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.events.RendererExistenceEvent;
	
	[Style(name="borderColor", type="uint", format="Color", inherit="no", theme="spark")]
	[Style(name="borderVisible", type="Boolean", inherit="no", theme="spark")]
	[Style(name="borderAlpha", type="Number", inherit="no", theme="spark", minValue="0.0", maxValue="1.0")]
	
	public class UploadFiles extends SkinnableComponent
	{
		
		public function UploadFiles()
		{
			super();
			_filelist.addEventListener(Event.SELECT,selectFileHandler);
		}
		
		//文件选择集合
		private var _filelist:FileReferenceList = new FileReferenceList();
		/**
		 * 所选择的文件集合
		 * */
		private var _files:ArrayCollection = new ArrayCollection();
		/**
		 * 选中的所有文件的大小
		 * */
		private var _filesSize:int = 0;
		/**
		 * 选中的文件的数量
		 * */
		private var _filesCount:int = 0;
		
		
		/**
		 * 是否同步上传
		 **/
		public var synchro:Boolean = true;
		
		/**
		 * 选择文件时的过滤器数组
		 * */
		public var filter:Array = null;
		
		/**
		 * 单个文件的最大文件大小，单位是 Byte，默认为-1,不限制。
		 * */
		public var fileMaxSize:int=-1;
		
		
		
		
		[SkinPart(required="false")]
		public var browseBtn:Button;
		[SkinPart(required="false")]
		public var fileCountLbl:Label;
		[SkinPart(required="false")]
		public var fileSizeLbl:Label;
		[SkinPart(required="false")]
		public var scroller:Scroller;
		[SkinPart(required="false")]
		public var dataGroup:DataGroup;
		
		
		// 选择文件的事件
		private function selectFileHandler(e:Event):void{
			var isShowInfo:Boolean = false;
			for each(var file:FileReference in _filelist.fileList){
				if(fileMaxSize>-1 && file.size>fileMaxSize){
//					infoStr += "\n"+file.name;
					isShowInfo = true;
				}else{
					_filesSize += file.size;
					var fo:SetariaFile = new SetariaFile();
					fo.file = file;
					fo.state = SetariaFile.STATE_WAIT;
					_files.addItem(fo);
				}
			}
			if(isShowInfo){
//				MessageShow.msgShow("系统提示","",infoStr);
			}
			setShowSize();
			_filesCount = _files.length;
		}
		//格式化文件总大小
		private function setShowSize():void{
			if(_filesSize<=0){
				fileSizeLbl.text = "总大小：0MB";
			}else if(_filesSize<1024*1024){
				fileSizeLbl.text = "总大小：小于1MB";
			}else{
				fileSizeLbl.text = "总大小："+(_filesSize/1024/1024).toFixed(2)+"MB";
			}
		}
		
		override protected function commitProperties():void{
			super.commitProperties();	
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			if(dataGroup){
				dataGroup.dataProvider = _files;
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			if (instance == dataGroup)
			{
				dataGroup.addEventListener(
					RendererExistenceEvent.RENDERER_ADD, dataGroup_rendererAddHandler);
				dataGroup.addEventListener(
					RendererExistenceEvent.RENDERER_REMOVE, dataGroup_rendererRemoveHandler);
			}
			else if (instance == scroller)
			{
				scroller.hasFocusableChildren = hasFocusableChildren;
			}
			else if (instance == browseBtn)
			{
				browseBtn.label = "浏览...";
				browseBtn.addEventListener(MouseEvent.CLICK,browseHandler);
			}
			else if (instance == fileCountLbl)
			{
				fileCountLbl.text = "文件个数：0个";
			}
			else if (instance == fileSizeLbl)
			{
				fileSizeLbl.text = "总大小：0KB";
			}
			
		}
		
		private function dataGroup_rendererAddHandler(event:RendererExistenceEvent):void
		{
		}
		
		private function dataGroup_rendererRemoveHandler(event:RendererExistenceEvent):void
		{
		}
		
		private function browseHandler(e:MouseEvent):void{
			if( filter == null || filter.length == 0 )
				_filelist.browse();
			else
				_filelist.browse(filter);
		}
	}
}